﻿using System;
using NUnit.Framework;

namespace Simhopp.Common
{
    /// <summary>
    /// A test class for the Competition class.
    /// </summary>
    [TestFixture]
    class TestCompetition
    {
        #region FIELD REGION
        private Competition TestC;

        private Diver TestD1;
        private Diver TestD2;

        private Dive TestDIVE1;
        private Dive TestDIVE2;
        private Dive TestDIVE3;

        private User TestJ1;
        private User TestJ2;
        private User TestJ3;
        private User TestJ4;
        private User TestJ5;

        private Score TestScore1;
        private Score TestScore2;
        private Score TestScore3;
        private Score TestScore4;
        private Score TestScore5;
        #endregion

        #region TEST METHOD REGION
        [SetUp]
        public void Init()
        {
            TestC = new Competition();

            TestC.NumberOfJudges = 5;


            TestD1 = new Diver { Name = "TestD1" };
            TestD2 = new Diver { Name = "TestD2" };

            TestDIVE1 = new Dive
            {
                TypeName = "nono"
            };
            TestDIVE2 = new Dive
            {
                TypeName = "nono"
            };
            TestDIVE3 = new Dive
            {
                TypeName = "nono"
            };

            TestJ1 = new User
            {
                UserName = "ankan",
                Name = "Anders Andersson",
                Age = 45,
                Nationality = "Sweden",
                Gender = Genders.Male,
                AccountType = AccountTypes.Judge,
                SocialSecurityNumber = "33"
            };
            TestJ2 = new User
            {
                UserName = "lasse",
                Name = "Lars Larsson",
                Age = 42,
                Nationality = "Sweden",
                Gender = Genders.Male,
                AccountType = AccountTypes.Judge,
                SocialSecurityNumber = "33"
            };
            TestJ3 = new User
            {
                UserName = "göran",
                Name = "Göran Göransson",
                Age = 32,
                Nationality = "Sweden",
                Gender = Genders.Male,
                AccountType = AccountTypes.Judge,
                SocialSecurityNumber = "44"
            };
            TestJ4 = new User
            {
                UserName = "judy",
                Name = "Judy Judeson",
                Age = 31,
                Nationality = "England",
                Gender = Genders.Female,
                AccountType = AccountTypes.Judge,
                SocialSecurityNumber = "55"
            };
            TestJ5 = new User
            {
                UserName = "gunvor",
                Name = "Gunvor Gunvorsdotter",
                Age = 55,
                Nationality = "Sweden",
                Gender = Genders.Female,
                AccountType = AccountTypes.HeadJudge,
                SocialSecurityNumber = "66"
            };


            TestJ1.AccountType = AccountTypes.Judge;
            TestJ2.AccountType = AccountTypes.Judge;
            TestJ3.AccountType = AccountTypes.Judge;
            TestJ4.AccountType = AccountTypes.Judge;
            TestJ5.AccountType = AccountTypes.Judge;




            TestScore1 = new Score(TestJ1, 7.5);
            TestScore2 = new Score(TestJ2, 3.5);
            TestScore3 = new Score(TestJ3, 4.5);
            TestScore4 = new Score(TestJ4, 6.0);
            TestScore5 = new Score(TestJ5, 9.0);

            TestDIVE1.AddScore(TestScore1);
            TestDIVE1.AddScore(TestScore2);
            TestDIVE1.AddScore(TestScore3);
            TestDIVE1.AddScore(TestScore4);
            TestDIVE1.AddScore(TestScore5);

            TestD1.Dives[0] = TestDIVE1;
            TestD1.Dives[1] = TestDIVE2;
            TestD1.Dives[2] = TestDIVE3;

            TestC.AddJudge(TestJ1);
            TestC.AddJudge(TestJ2);
            TestC.AddJudge(TestJ3);
            TestC.AddJudge(TestJ4);
            TestC.AddJudge(TestJ5);

            TestC.AddParticipant(TestD1);

            TestC.Heights[0] = (Heights.OneMeter);
            TestC.Heights[1] = (Heights.ThreeMeters);
            TestC.Heights[2] = (Heights.TenMeters);
        }

        [Test]
        public void TestStartConfigured()
        {
            TestC.NextRound();
            Assert.AreEqual(Rounds.FirstRound, TestC.CurrentRound);
        }

        [Test, ExpectedException(typeof (ArgumentException))]
        public void TestStartNotEnoughJudges()
        {
            TestC.RemoveJudge(TestJ1);
            TestC.NextRound();
        }

        [Test, ExpectedException(typeof(ArgumentException))]
        public void TestStartNotEnoughDives()
        {
            TestD1.RemoveDive(TestDIVE1);
            TestC.NextRound();
        }

        [Test, ExpectedException(typeof(ArgumentException))]
        public void TestProceedToRoundTwoNotComplete()
        {
            TestC.NextRound();
            TestDIVE1.RemoveScore(TestScore1);
            TestC.NextRound();
        }

        [Test]
        public void TestProceedToRoundTwoComplete()
        {
            TestC.NextRound();
            TestC.NextRound();
            Assert.AreEqual(Rounds.SecondRound, TestC.CurrentRound);
        }

        [Test, ExpectedException(typeof(ArgumentException))]
        public void TestProceedToRoundOverflow()
        {
            TestC.NextRound();
            TestC.NextRound();
            TestC.NextRound();
            TestC.NextRound();
        }

        [Test]
        public void TestReadFile()
        {
            Competition c = new Competition();

            c.LoadDivingTypesFromFile();

            foreach (var divetype in Competition.DivingTypeList)
            {
                Console.WriteLine(divetype.KeyId + " " + divetype.ID + " " + divetype.Height);


            }

            Assert.IsNotEmpty(Competition.DivingTypeList);
        }


        [Test]
        public void TestAddParticipants()
        {
            TestC.AddParticipant(TestD2);

            Assert.AreEqual(TestC.Participants[0], TestD1);
            Assert.AreEqual(TestC.Participants[1], TestD2);
        }


        [Test, ExpectedException(typeof(ArgumentException))]
        public void TestGender()
        {
            TestD2.Gender = Genders.Female;

            TestC.AddParticipant(TestD2);
        }


        [Test, ExpectedException(typeof(ArgumentException))]
        public void TestCurrentDiverConfiguring()
        {
            TestC.GetCurrentDiver();
        }

        [Test]
        public void TestNextDiver()
        {
            TestC.AddParticipant(TestD2); 

            Assert.AreEqual(TestD2, TestC.GetNextDiver());
            
        }


        [Test, ExpectedException(typeof(ArgumentException))]
        public void TestNextDiverOverflow()
        {
            TestC.NextRound();

            TestC.GetNextDiver();
        }


        [Test]
        public void TestChangeGender()
        {
            TestC.AddParticipant(TestD2);

            TestC.Gender = Genders.Female;

            Assert.IsEmpty(TestC.Participants);
        }


        [Test]
        public void TestRemoveDiver()
        {
            TestC.AddParticipant(TestD2);

            Assert.IsTrue((TestC.RemoveParticipant(TestD1)));

            Assert.AreEqual(TestD2, TestC.Participants[0]);
        }

        [Test, ExpectedException(typeof(JudgeListFullException))]
        public void TestAddJudge()
        {
            TestC.AddJudge(TestJ1);

            Assert.AreEqual(TestJ1, TestC.Judges[0]);
        }

        [TearDown]
        public void Terminate()
        {
        }
        #endregion

    }
}
